#include "log.h"
#include <QMessageBox>
#include <QDebug>
#include <QHostAddress>
#include "ui_log.h"

Log::Log(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Log)
{
    ui->setupUi(this);
    this->status = new QLabel; 
    status->setPixmap(QPixmap(":/img/disconnect.jpg").scaled(30, 30)); 
    ui->statusbar->addWidget(new QLabel("连接状态")); 
    ui->statusbar->addWidget(this->status);    

    this->cfd = new QTcpSocket; 
    cfd->connectToHost(QHostAddress(this->ip), this->port);

    connect(cfd, &QTcpSocket::readyRead, this, &Log::recv_Msg); 

    connect(ui->rB_anonymous, &QRadioButton::clicked, this, &Log::show_Pag); 
    connect(ui->rB_login, &QRadioButton::clicked, this, &Log::show_Pag); 
    connect(ui->rB_register, &QRadioButton::clicked, this, &Log::show_Pag); 

    connect(ui->btn_anonymous, &QPushButton::clicked, this, [=]()
    {
        this->cfd->write("1"); 
    }); 

    connect(cfd, &QTcpSocket::disconnected, this, &Log::disconnect_ok); 

    connect(ui->btn_login, &QPushButton::clicked, this, Log::login); 
    connect(ui->btn_register, &QPushButton::clicked, this, &Log::slot_register); 
}

Log::~Log()
{
    qDebug() << "Log dead"; 
    delete ui;
    qDebug() << "t001"; 
    this->cfd->close(); 
    qDebug() << "t002"; 
    delete cfd; 
    qDebug() << "t003"; 
    delete this->status; 
    qDebug() << "t004"; 
}

void Log::show_Pag()
{
    if(this->ui->rB_anonymous->isChecked()) this->ui->stackedWidget->setCurrentIndex(0); 
    if(this->ui->rB_login->isChecked()) this->ui->stackedWidget->setCurrentIndex(1); 
    if(this->ui->rB_register->isChecked()) this->ui->stackedWidget->setCurrentIndex(2); 
}

void Log::recv_Msg()
{
    char flag[4];
    this->cfd->read(flag, 3); 
    flag[3] = '\0'; 
    QString f(flag), msg; 
    if(f == "111")  
    {
        qDebug() << f;    
        this->status->setPixmap(QPixmap(":/img/connect.jpg").scaled(30, 30)); 
        msg = this->cfd->readAll(); 
        qDebug() << msg; 
    }
    else if(f == "010")
    {
        qDebug() << "uid";
        msg =  this->cfd->readAll(); 
        qDebug() << msg; 
        this->cfd->write(this->uid.toUtf8() + "\n"); 
    }
    else if(f == "001")
    {
        qDebug() << "uid错误"; 
        msg =  this->cfd->readAll(); 
        QMessageBox::warning(this, "重试", msg); 
        this->cfd->close();
        this->cfd->connectToHost(QHostAddress(this->ip), this->port);
        qDebug() << msg; 
    }
    else if(f == "002")
    {
        qDebug() << "已登陆"; 
        msg = this->cfd->readAll(); 
        QMessageBox::warning(this, "重试", msg); 
        this->cfd->close();
        this->cfd->connectToHost(QHostAddress(this->ip), this->port);
    }
    else if(f == "003")
    {
        qDebug() << "passwd"; 
        qDebug() << this->passwd; 
        this->cfd->readAll();
        this->cfd->write(this->passwd.toUtf8() + "\n"); 
    }
    else if(f == "004")
    {
        qDebug() << "登录成功"; 
        msg =  this->cfd->readAll(); 
        qDebug() << msg; 
    }
    else if(f == "011") 
    {
        msg =  this->cfd->readAll(); 
        disconnect(cfd, &QTcpSocket::readyRead, this, &Log::recv_Msg); 
        emit login_ok(msg, cfd); 
    }
    else if(f == "005") 
    {
        QMessageBox::warning(this, "重试", "密码输入错误"); 
        qDebug() << "密码错误"; 
        msg =  this->cfd->readAll(); 
        this->cfd->close();
        this->cfd->connectToHost(QHostAddress(this->ip), this->port);
        qDebug() << msg; 
    }
    else if(f == "###")
    {
        qDebug() << "注册1"; 
        msg = this->cfd->readAll(); 
        this->cfd->write(this->user_name.toUtf8() + "\n"); 
    }
    else if(f == "006") 
    {
        qDebug() << "注册密码"; 
        msg = this->cfd->readAll(); 
        this->cfd->write(this->rpasswd.toUtf8() + "\n"); 
    }
    else if(f == "007")
    {
        qDebug() << "再次输入注册密码"; 
        msg = this->cfd->readAll(); 
        this->cfd->write(this->rpasswd2.toUtf8() + "\n"); 
    }
    else if(f == "008") 
    {
        QMessageBox::warning(this, "错误", "两次密码不一致"); 
        msg =  this->cfd->readAll(); 
        this->cfd->close();
        this->cfd->connectToHost(QHostAddress(this->ip), this->port);
    }
    else if(f == "009")
    {
        qDebug() << "注册成功"; 
        msg =  this->cfd->readAll(); 
        QMessageBox::information(this, "注册成功", msg);  
        this->cfd->close();
        this->cfd->connectToHost(QHostAddress(this->ip), this->port);
    }
    else 
    {
        qDebug() << "else"; 
        msg =  this->cfd->readAll(); 
        qDebug() << msg; 
    }
}

void Log::login()
{
    this->uid = this->ui->lineEdit_uid->text(); 
    this->passwd = this->ui->lineEdit_password->text(); 
    if(uid.isEmpty() || passwd.isEmpty()) 
    {
        QMessageBox::warning(this, "错误", "请输入有效的账号和密码"); 
        return; 
    }
    this->cfd->write("2");
}

 
void Log::slot_register()
{
    user_name = this->ui->lineEdit_ruid->text(); 
    rpasswd = this->ui->lineEdit_rpassword->text(); 
    rpasswd2 = this->ui->lineEdit_rpassword2->text(); 
    if(user_name.isEmpty() || rpasswd.isEmpty() || rpasswd2.isEmpty()) 
    {
        QMessageBox::warning(this, "错误", "请填写完整"); 
        return; 
    }
    if(rpasswd2 != rpasswd) 
    {
        QMessageBox::warning(this, "错误", "两次密码不一致"); 
        return; 
    }
    this->cfd->write("3");
}